package com.techstar.roma.http;

import java.io.BufferedReader;
import java.io.FileReader;
import java.net.URI;
import java.util.Arrays;
import java.util.Properties;

import org.apache.http.client.methods.HttpRequestBase;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;

import com.techstar.api.service.GateWayApiProperties;
import com.techstar.api.service.HttpClientHeper;
import com.techstar.api.service.TrafficApiProperties;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class GatewayService {
	
	public static HttpRequestBase GateWay(HttpRequestBase httpRequestBase,GateWayApiProperties gateWayApiProperties,TrafficApiProperties trafficApiProperties) {
		try {
			String url = trafficApiProperties.getUrl();
			String host = gateWayApiProperties.getHost();
			String param = trafficApiProperties.getParam();
			String time = DateTime.now(DateTimeZone.UTC).toString("yyyyMMdd'T'HHmmss'Z'");
			String timeTag = gateWayApiProperties.getTimeTag();
			String heard = gateWayApiProperties.getHeard();
			String bodySHA256 = SHA256Util.SHA256(trafficApiProperties.getBody());
			String sha256Tag = gateWayApiProperties.getSha256Tag();
			String authorization = gateWayApiProperties.getAuthorization();
//			URL netUrl = new URL("http://"+host+url+(params.length()>0 ? "?"+params : params));
//			URI uri = new URI(netUrl.getProtocol(), netUrl.getHost(), netUrl.getPath(), netUrl.getQuery(), null);
//			params = uri.getRawQuery();
			httpRequestBase.setURI(URI.create("http://"+host+url+(param.length()>0 ? "?"+param : param)));
//			httpRequestBase.setURI(uri);
			StringBuffer gateWayFirst = new StringBuffer();
			gateWayFirst.append(httpRequestBase.getMethod()+"\n"+url+"/\n"+param+"\nhost:"+host+"\n"+timeTag+time+"\n\n"+heard+"\n"+bodySHA256);
			String SHA256 = SHA256Util.SHA256(gateWayFirst.toString());
			StringBuffer gateWaySecond = new StringBuffer();
			gateWaySecond.append(sha256Tag+"\n"+time+"\n"+SHA256);
			String HmacSHA256 = SHA256Util.HMACSHA256(gateWaySecond.toString(),gateWayApiProperties.getSecret());
			log.info(trafficApiProperties.toString());
			log.info(gateWayApiProperties.toString());
			log.info(gateWayFirst.toString());
			log.info(gateWaySecond.toString());
			HttpClientHeper.instance.buildHead(httpRequestBase, time, host, authorization+HmacSHA256.toLowerCase());
			log.info(param);
			log.info(httpRequestBase.getURI().toString());
			log.info(httpRequestBase.getConfig().toString());
			log.info(Arrays.toString(httpRequestBase.getAllHeaders()));
			return httpRequestBase;
		} catch (Exception e) {
			e.printStackTrace();
			return httpRequestBase;
		}
	}
	
	
	public static void main(String[] args) throws Exception {
		Properties properties = new Properties();
	    // 使用InPutStream流读取properties文件
	    BufferedReader bufferedReader = new BufferedReader(new FileReader("F:\\WorkSpace\\eclipse\\HT2020\\sz-traffic\\traffic-ioc\\traffic-ioc-master\\src\\main\\resources\\application.properties"));
	    properties.load(bufferedReader);
	    // 获取key对应的value值
	    String params = properties.getProperty("traffic.api.demo.params");
	    boolean mode = properties.contains("traffic.roma-api.testMode");
	    System.out.println(params);
	    System.out.println(mode);
	    System.out.println("56F677F5B585E41BFECFF9EFCA297F2BC9E82F639ABB1D568D36C21C539E8B53".toLowerCase());
	}
}
